from decimal import Decimal
from datetime import datetime, date
from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel
from pydantic_validation_decorator import NotBlank
from typing import Optional
from module_admin.annotation.pydantic_annotation import as_query

class KlineModel(BaseModel):
    """
    股票k线表对应pydantic模型
    """
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)

    id: Optional[int] = Field(default=None, description='id')
    trade_date: Optional[date] = Field(default=None, description='交易日期')
    symbol: Optional[str] = Field(default=None, description='股票代码')
    open: Optional[Decimal] = Field(default=None, description='开盘价')
    close: Optional[Decimal] = Field(default=None, description='收盘价')
    high: Optional[Decimal] = Field(default=None, description='最高价')
    low: Optional[Decimal] = Field(default=None, description='最低价')
    vol: Optional[Decimal] = Field(default=None, description='成交量')
    amount: Optional[Decimal] = Field(default=None, description='成交额')
    amplitude: Optional[Decimal] = Field(default=None, description='振幅')
    pct_chg: Optional[Decimal] = Field(default=None, description='涨跌幅')
    chg: Optional[Decimal] = Field(default=None, description='涨跌额')
    turn: Optional[Decimal] = Field(default=None, description='换手率')
    create_time: Optional[datetime] = Field(default=None, description='创建时间')
    update_time: Optional[datetime] = Field(default=None, description='更新时间')

    @NotBlank(field_name='trade_date', message='交易日期不能为空')
    def get_trade_date(self):
        return self.trade_date

    @NotBlank(field_name='symbol', message='股票代码不能为空')
    def get_symbol(self):
        return self.symbol

    @NotBlank(field_name='create_time', message='创建时间不能为空')
    def get_create_time(self):
        return self.create_time

    @NotBlank(field_name='update_time', message='更新时间不能为空')
    def get_update_time(self):
        return self.update_time

    def validate_fields(self):
        self.get_trade_date()
        self.get_symbol()
        self.get_create_time()
        self.get_update_time()




class KlineQueryModel(KlineModel):
    """
    股票k线不分页查询模型
    """
    pass


@as_query
class KlinePageQueryModel(KlineQueryModel):
    """
    股票k线分页查询模型
    """

    page_num: int = Field(default=1, description='当前页码')
    page_size: int = Field(default=10, description='每页记录数')


class DeleteKlineModel(BaseModel):
    """
    删除股票k线模型
    """

    model_config = ConfigDict(alias_generator=to_camel)

    ids: str = Field(description='需要删除的id')
